package com.umeox.watch.moto.dataservice.proxy;

import android.os.IBinder;
import android.os.RemoteException;

import com.umeox.watch.moto.apiclient.model.AgentAppConfigDTO;
import com.umeox.watch.moto.apiclient.model.AgentUsedRecord;
import com.umeox.watch.moto.apiclient.model.ChantsCounterDTO;
import com.umeox.watch.moto.apiclient.model.GetFriendsAttrDTO;
import com.umeox.watch.moto.apiclient.model.StepRankingDTO;
import com.umeox.watch.moto.apiclient.model.StoryDTO;
import com.umeox.watch.moto.apiclient.model.TaskRecordInfoDTO;
import com.umeox.watch.moto.apiclient.model.UserAgentDTO;
import com.umeox.watch.moto.dataservice.BuildConfig;
import com.umeox.watch.moto.dataservice.interfaces.IPushApiClient;

import net.vidageek.mirror.dsl.Mirror;

import java.util.ArrayList;
import java.util.List;

public class PushApiClientProxy implements IPushApiClient {
    private IBinder mRemote;

    private PushApiClientProxy(IBinder remote) {
        mRemote = remote;
    }

    public static IPushApiClient fetchBinder() {
        try {
            return new PushApiClientProxy((IBinder) new Mirror().on("android.os.ServiceManager").invoke().method("getService").withArgs(BuildConfig.pushApiBinderName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public IBinder asBinder() {
        return mRemote;
    }

    @Override
    public void serverConfig() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_SERVER_CONFIG, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public void deviceConfig() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_DEVICE_CONFIG, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public void bind() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_BIND, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public void unbind() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_UNBIND, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public void requestGetContacts() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_REQUEST_GET_CONTACTS, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public void requestGetBuddies() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_REQUEST_GET_BUDDIES, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public void locationPeriodsConfig() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_LOCTION_PERIODS_CONFIG, data, reply, 0);
            reply.readException();
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public void forbidPeriodsConfig() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_FORBID_PERIODS_CONFIG, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public void requestGetAlarmClocks() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_REQUEST_GET_ALARM_CLOCKS, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public void requestGetWifiSettings() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_REQUEST_GET_WIFI_SETTINGS, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public void requestGetWallpapers() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_REQUEST_GET_WALLPAPERS, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public void requestGetStepInfo() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_REQUEST_GET_STEPINFO, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public void requestGetBindCode() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_REQUEST_GET_BINDCODE, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public List<StoryDTO> getStoriesPage() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        List<StoryDTO> storyDTOList = new ArrayList<>();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_GET_STORIES_PAGE, data, reply, 0);
            reply.readException();
            reply.readTypedList(storyDTOList,StoryDTO.CREATOR);
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }

        return storyDTOList;
    }

    @Override
    public List<StoryDTO> getRecommendStories() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        List<StoryDTO> storyDTOList = new ArrayList<>();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_GET_RECOMMEND_STORIES, data, reply, 0);
            reply.readException();
            reply.readTypedList(storyDTOList,StoryDTO.CREATOR);
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }

        return storyDTOList;
    }

    @Override
    public StepRankingDTO getStepRanking() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        StepRankingDTO stepRankingDTO = null;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_GET_STEP_RANKING, data, reply, 0);
            reply.readException();
            stepRankingDTO = StepRankingDTO.CREATOR.createFromParcel(reply);
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }

        return stepRankingDTO;
    }

    @Override
    public TaskRecordInfoDTO getTaskRecord() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        TaskRecordInfoDTO taskRecordInfoDTO = null;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_GET_TASK_RECORD, data, reply, 0);
            reply.readException();
            taskRecordInfoDTO = TaskRecordInfoDTO.CREATOR.createFromParcel(reply);
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }

        return taskRecordInfoDTO;
    }

    @Override
    public ChantsCounterDTO getChantsCounter() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        ChantsCounterDTO chantsCounterDTO = null;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_GET_CHANTS_COUNTER, data, reply, 0);
            reply.readException();
            chantsCounterDTO = ChantsCounterDTO.CREATOR.createFromParcel(reply);
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }

        return chantsCounterDTO;
    }

    @Override
    public GetFriendsAttrDTO getFriendsAttr() throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        GetFriendsAttrDTO getFriendsAttrDTO = null;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            mRemote.transact(TRANSACTION_GET_FRIENDS_ATTR, data, reply, 0);
            reply.readException();
            getFriendsAttrDTO = GetFriendsAttrDTO.CREATOR.createFromParcel(reply);
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }

        return getFriendsAttrDTO;
    }

    @Override
    public void createGroup(String groupName, List<String> friendIds) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(groupName);
            data.writeStringList(friendIds);
            mRemote.transact(TRANSACTION_CREATE_GROUP, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public void updateGroup(String groupId, String groupName) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(groupId);
            data.writeString(groupName);
            mRemote.transact(TRANSACTION_UPDATE_GROUP, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public void inviteJoinGroup(String groupId, List<String> friendIds) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(groupId);
            data.writeStringList(friendIds);
            mRemote.transact(TRANSACTION_INVITE_JOIN_GROUP, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public void removeGroupMember(String groupId, List<String> friendIds) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(groupId);
            data.writeStringList(friendIds);
            mRemote.transact(TRANSACTION_REMOVE_GROUP_MEMBER, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public void quitGroup(String groupId) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(groupId);
            mRemote.transact(TRANSACTION_QUIT_GROUP, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }

    @Override
    public UserAgentDTO getAgentUser(String appKey, String agentId, String userId) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        UserAgentDTO userAgentDTO = null;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(appKey);
            data.writeString(agentId);
            data.writeString(userId);
            mRemote.transact(TRANSACTION_GET_AGENT_USER, data, reply, 0);
            reply.readException();
            userAgentDTO = UserAgentDTO.CREATOR.createFromParcel(reply);
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return userAgentDTO;
    }

    @Override
    public AgentAppConfigDTO getAgentAppConfig(String appKey, String userId, int type) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        AgentAppConfigDTO agentAppConfigDTO = null;
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(appKey);
            data.writeString(userId);
            data.writeInt(type);
            mRemote.transact(TRANSACTION_GET_AGENT_APP_CONFIG, data, reply, 0);
            reply.readException();
            agentAppConfigDTO = AgentAppConfigDTO.CREATOR.createFromParcel(reply);
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
        return agentAppConfigDTO;
    }

    @Override
    public void submitAgentUsedRecord(String appKey, String userId, List<AgentUsedRecord> details) throws RemoteException {
        android.os.Parcel data = android.os.Parcel.obtain();
        android.os.Parcel reply = android.os.Parcel.obtain();
        try {
            data.writeInterfaceToken(DESCRIPTOR);
            data.writeString(appKey);
            data.writeString(userId);
            data.writeTypedList(details);
            mRemote.transact(TRANSACTION_SUBMIT_AGENT_USED_RECORD, data, reply, 0);
            reply.readException();
        } catch (Exception e) {
            throw new RemoteException(e.getMessage());
        } finally {
            reply.recycle();
            data.recycle();
        }
    }
}
